import GuideData from "../data/GuideData";
import { CommonEvents } from "../net/CommonEvents";
import DebugUtil from "../utils/DebugUtil";
import LogUtil from "../utils/LogUtil";
import EventManager from "./EventManager";
import ResManager from "./ResManager";
import SceneNavigator from "./SceneNavigator";
import { UIManager } from "./UIManager";

const {ccclass} = cc._decorator;

@ccclass
export default abstract class BaseScene extends cc.Component {

    private uiRoot: cc.Node;
    private tctNode: cc.Node;

    private rootNodes = ['uiRoot','popRoot','loadRoot','guideRoot'];
    
    onLoad() {
        cc.view.setOrientation(cc.macro.ORIENTATION_LANDSCAPE);
        this.fixApdater();
        this.initUIRoot();
        //场景返回按钮
        let back = cc.find('Canvas/back');
        if(back){
            back.on(cc.Node.EventType.TOUCH_END, ()=>{
                LogUtil.log('backHome>>');
                SceneNavigator.goHome({});
            }, this);
        }
        //添加UILoading
        let UILoading = cc.find('Canvas/UILoading');
        if(!UILoading){
            LogUtil.log(LogUtil.getFileName(),"init UILoading");
            UIManager.ins().show('prefabs/UILoading');
        }

        EventManager.on(CommonEvents.GUIDE_SHOW,this.showGuide,this);

        this.showTextureCache();

        DebugUtil.setStatsColor(cc.Color.RED);
    }

    onDestroy() {
        EventManager.off(CommonEvents.GUIDE_SHOW,this.showGuide,this);
    }

    /**
     * 适配
     */
    private fixApdater() {
        let framesize = cc.view.getFrameSize();
        let designsize = cc.view.getDesignResolutionSize();
        let canvas: cc.Canvas = cc.Canvas.instance;
        
        let ratio: number = framesize.height / framesize.width;
        let designRatio: number = designsize.height / designsize.width;
        if (ratio > designRatio) {
            canvas.fitHeight = false;
            canvas.fitWidth = true;
        } else {
            canvas.fitHeight = true;
            canvas.fitWidth = false;
        }
    }

    /**
     * 添加UIRoot节点
     */
    private initUIRoot() {
        let uiRoot = cc.find('Canvas/UIRoot');
        if(!uiRoot){
            this.uiRoot = new cc.Node('UIRoot');
            this.uiRoot.x = 0;
            this.uiRoot.y = 0;
            this.uiRoot.width = cc.winSize.width;
            this.uiRoot.height = cc.winSize.height;
            this.uiRoot.setParent(cc.Canvas.instance.node);

            const widget = this.uiRoot.addComponent(cc.Widget);
            widget.alignMode = cc.Widget.AlignMode.ALWAYS;
            widget.bottom = widget.top = widget.left = widget.right = 0;
        }
    }

    /**
     * 展示引导界面
     * @param groupId 
     * @param smallId 
     */
    private showGuide(groupId, smallId) {
        let options = {
            groupId:groupId, 
            smallId:smallId
        }
        if(GuideData.ins().checkHasGuide(groupId, smallId)){
            if(UIManager.ins().uiGuide){
                LogUtil.log('uiGuide>>> 已存在');
                UIManager.ins().uiGuide.init(options);
            }else{
                UIManager.ins().show('prefabs/UIGuide',options);
            }
        }
        
    }

    private showTextureCache() {
        let tip = cc.find('Canvas/TextureCacheTip');
        if(!tip){
            this.tctNode = new cc.Node('TextureCacheTip');
            this.tctNode.x = 0;
            this.tctNode.y = cc.winSize.height/2 - 50;
            this.tctNode.color = new cc.Color(255,0,0,0);
            this.tctNode.setParent(cc.Canvas.instance.node);
            this.tctNode.addComponent(cc.Label);
            let lab = this.tctNode.getComponent(cc.Label);
            lab.fontSize = 30;
            lab.string = ResManager.ins().computeTextureCache()+' 点击刷新';
            this.tctNode.width = ResManager.ins().computeTextureCache().length+150;
            this.tctNode.height = 50;
            this.tctNode.on(cc.Node.EventType.TOUCH_END, ()=>{
                lab.string = ResManager.ins().computeTextureCache()+' 点击刷新';;
            }, this);
        }
    }
}